Add support for mypy - #31
Conversation
27baa86 to
62dc90e
Compare
|
Hi, @moi15moi The primary reason these type checker errors occur is that there is a discrepancy between the members dynamically defined by metaprogramming using You can potentially resolve these errors by addressing the following points:
If I were to extract a portion of this project's codebase and rewrite it to align more closely with the typed Python style, it might look like the following. I hope this serves as a helpful reference. class IDWriteFactory(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefactory
_iid_ = GUID("{b859ee5a-d838-4b5b-a2e8-1adc7d93db48}")
_methods_ = [
...
STDMETHOD(HRESULT, "RegisterFontFileLoader", [POINTER(IDWriteFontFileLoader)]),
...
STDMETHOD(HRESULT, "GetGdiInterop", [POINTER(POINTER(IDWriteGdiInterop))]),
...
]
+ if TYPE_CHECKING:
+ def RegisterFontFileLoader(self, fontfileloader: IDWriteFontFileLoader) -> int: ...
+
+ def GetGdiInterop(self) -> IDWriteGdiInterop:
+ ptr = POINTER(IDWriteGdiInterop)()
+ self.__com_GetGdiInterop(byref(ptr))
+ return ptr # type: ignore
+_T_IUnknown = TypeVar("_T_IUnknown", bound=IUnknown)
class DWrite:
def __init__(self) -> None:
dwrite = windll.LoadLibrary("dwrite")
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nf-dwrite-dwritecreatefactory
- self.DWriteCreateFactory = dwrite.DWriteCreateFactory
- self.DWriteCreateFactory.restype = HRESULT
- self.DWriteCreateFactory.argtypes = [wintypes.UINT, POINTER(IID), POINTER(POINTER(IUnknown))]
+ self._DWriteCreateFactory = dwrite.DWriteCreateFactory
+ self._DWriteCreateFactory.restype = HRESULT
+ self._DWriteCreateFactory.argtypes = [wintypes.UINT, POINTER(IID), POINTER(POINTER(IUnknown))]
+
+ def DWriteCreateFactory(self, factorytype: int, interface: Type[_T_IUnknown], /) -> _T_IUnknown:
+ ptr = POINTER(interface)()
+ self._DWriteCreateFactory(factorytype, byref(interface._iid_), byref(ptr))
+ return ptr # type: ignore
dwrite = DWrite()
-dwrite_factory = POINTER(IDWriteFactory)()
-dwrite.DWriteCreateFactory(DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_ISOLATED, byref(dwrite_factory._iid_), byref(dwrite_factory))
-gdi_interop = POINTER(IDWriteGdiInterop)()
-dwrite_factory.GetGdiInterop(byref(gdi_interop))
+dwrite_factory = dwrite.DWriteCreateFactory(DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_ISOLATED, IDWriteFactory)
+gdi_interop = dwrite_factory.GetGdiInterop() |
|
Thank you very much for your detailed answer! |
@junkmd Do you have any idea how I can remove all the typing errors caused by
comtypes?Currently, I simply ignore all errors that look like this:
However, I was wondering if there’s a way to properly fix these errors. Even if I replace
POINTER[...]withPOINTER(...), I still get other errors, such as:Would appreciate any insights!